# Copyright 2011 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Copyright 2012 Google Inc. All Rights Reserved.
#
# Permutation with repetition is easy. Create all possible swizzler
# functions as methods on the Expression class. This really only makes
# sense performancewise if we use the closure compiler to eliminate
# unused function definitions.
#
#
def permutate(str)
  ps = [];

  perm = lambda do |r, tmp|
    str.each_char do |c|
      tmp[r] = c
      if (r == 0)
        ps << String.new(tmp)
      else
        perm.call(r-1, tmp)
      end
    end
  end

  (0...str.size).each do |i|
    tmp = Array.new(i+1, '0').join
    perm.call(i, tmp)
  end

  return ps
end

def generate(name)
  <<EOF
/**
 * @return {!embedsl.Expression} Swizzler #{name}.
 */
embedsl.Expression.prototype.#{name} = function() {
  // Generated by #{$0}. Do not edit.
  return new embedsl.Expression(
      embedsl.Kind.SWIZZLER,
      embedsl.TypeInfo.SameBaseDim#{name.size},
      '#{name}',
      null,
      [this]);
};
EOF
end

puts <<EOT
/*
 * All swizzeling functions generated by #{$0}. Do not edit.
 */

EOT

['xyzw', 'rgba', 'stpq'].each do |set|
  permutate(set).each do |name|
    puts generate(name)
  end
end
